﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)


        {
            String user = txtUser.Text.ToString();
            String pass = txtPass.Text.ToString();

            if (user.Length <= 0 && pass.Length <= 0)
            {
                MessageBox.Show(new Form() { TopMost = true }, "Please Enter Valid User Name or Password ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                try
                {
                    String getUser = "SELECT COUNT(UserID),Role FROM loggin WHERE UserID = '" + user + "' AND password = '" + pass + "' GROUP BY Role";
                    int lc = 0;
                    String role = "";

                    SqlDataReader ldr = DbConnet.ReadTable(getUser);
                    if (ldr.Read())
                    {
                        lc = int.Parse(ldr[0].ToString());
                        role = ldr["Role"].ToString();
                    }
                    ldr.Close();

                    Midval m = new Midval();
                    m.setUser(user);
                    m.setRole(role);


                    if (lc <= 0)
                    {
                        MessageBox.Show(new Form() { TopMost = true }, "Invalid Credentials Please Try Again ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        new MDI().Show();
                        this.Hide();
                    }
                    
                }
                catch (Exception exlog)
                {
                    MessageBox.Show(new Form() { TopMost = true }, "Invalid Credentials Please Try Again ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Log l = new Log();
            DbConnet.connect(l.getConstr());
        }
    }
}
